import { requestClient } from '#/api/request';

export namespace AuthApi {
  /** 登录接口参数 */
  export interface LoginParams {
    password?: string;
    username?: string;
  }

  /** 登录接口返回值 */
  export interface LoginResult {
    access_token: string;
    accessToken: string;
    refreshToken: string;
    refresh_token: string;
  }

  export interface RefreshTokenResult {
    data: string;
    status: number;
    access_token: string;
    refresh_token: string;
  }

  export interface CaptchaResult {
    phoneNumber: string;
    sendType: number;
  }
  export interface RegisterByPhone {
    password: string;
    phoneCode: string;
    phoneNumber: string;
    username: string;
  }

  export interface RALoginResult {
    access_token: string;
    accessToken: string;
  }
}

/**
 * 登录
 */
export async function loginApi(data: AuthApi.LoginParams) {
  return requestClient.post<AuthApi.LoginResult>('api/login', data);
}
/**
 * 验证码登陆
 */
export async function captchaLoginApi(data: AuthApi.LoginParams) {
  return requestClient.post<AuthApi.RALoginResult>('api/loginByPhone', data);
}

/**
 * token登陆
 */
export async function loginByToken(data: any) {
  return requestClient.post<AuthApi.RefreshTokenResult>(
    'api/loginByToken',
    data,
  );
}

/**
 * 刷新accessToken
 */
export async function refreshTokenApi(data: any) {
  return requestClient.post<AuthApi.RefreshTokenResult>('api/refresh', data);
}

/**
 * 退出登录
 */
export async function logoutApi() {
  return requestClient.delete('api/logout', {
    withCredentials: true,
  });
}

/**
 * 获取用户权限码
 */
export async function getAccessCodesApi() {
  return []; // requestClient.get<string[]>('/auth/codes');
}

/**
 * 获取验证码
 */
export async function getCaptchaApi(data: AuthApi.CaptchaResult) {
  return requestClient.post<string>('api/getCodeByPhone', data);
}
/**
 * 注册
 */
export async function registerApi(data: AuthApi.LoginParams) {
  return requestClient.post<AuthApi.RALoginResult>('api/registerByPhone', data);
}

/**
 * 店铺绑定手机号
 */
export async function shopBindPhoneApi(data: any) {
  return requestClient.post<any>('api/bindShop', data);
}
